﻿using System;
using System.Configuration;
using System.Collections.Specialized;
using System.ComponentModel;

namespace Fuse8.DomainFramework.Configuration.Cache
{
    /// <summary>
    /// 
    /// </summary>
    public class CacheContainerElement : ConfigurationElement
    {
        #region Fields

        private readonly NameValueCollection _settings = new NameValueCollection();

        #endregion

        #region Constructors

        /// <summary>
        /// 
        /// </summary>
        public CacheContainerElement()
        { }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="elementName"></param>
        public CacheContainerElement(string elementName)
        {
            Name = elementName;
        }

        #endregion

        #region Properties

        /// <summary>
        /// 
        /// </summary>
        [ConfigurationProperty("name", IsRequired = true, IsKey = true)]
        public string Name
        {
            get
            {
                return this["name"].ToString();
            }
            set
            {
                this["name"] = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        [ConfigurationProperty("main", IsRequired = false, DefaultValue = false)]
        public bool Main
        {
            get
            {
                return this["main"] != null ?
                    (bool)this["main"] : false;
            }
            set
            {
                this["main"] = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        [ConfigurationProperty("containerType", IsRequired = true)]
        public string ContainerType
        {
            get
            {
                return this["containerType"].ToString();
            }
            set
            {
                this["containerType"] = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        [ConfigurationProperty("successorContainerName", IsRequired = false)]
        public string SuccessorContainerName
        {
            get
            {
                return this["successorContainerName"].ToString();
            }
            set
            {
                this["successorContainerName"] = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        [ConfigurationProperty("forType", IsRequired = false)]
        public string ForType
        {
            get
            {
                return this["forType"].ToString();
            }
            set
            {
                this["forType"] = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        [ConfigurationProperty("dependency", IsRequired = false)]
        public string Dependency
        {
            get
            {
                return this["dependency"].ToString();
            }
            set
            {
                this["dependency"] = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        [ConfigurationProperty("enabled", IsRequired = false, DefaultValue = true)]
        public bool Enabled
        {
            get
            {
                return this["enabled"] != null ?
                    (bool)this["enabled"] : true;
            }
            set
            {
                this["enabled"] = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        [ConfigurationProperty("absoluteExpiration", IsRequired = false, DefaultValue = "00:00:00"), TimeSpanValidator, TypeConverter(typeof(InfiniteTimeSpanConverter))]
        public TimeSpan AbsoluteExpiration
        {
            get
            {
                return this["absoluteExpiration"] != null ?
                    (TimeSpan)this["absoluteExpiration"] :
                    TimeSpan.Zero;
            }
            set
            {
                this["absoluteExpiration"] = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        [ConfigurationProperty("async", IsRequired = false, DefaultValue = true)]
        public bool Async
        {
            get
            {
                return this["async"] != null ?
                    (bool)this["async"] : true;
            }
            set
            {
                this["async"] = value;
            }
        }

        /// <summary>
        /// Another settings
        /// </summary>
        public NameValueCollection Settings
        {
            get
            {
                return _settings;
            }
        }        

        #endregion

        #region Override methods

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        protected override bool OnDeserializeUnrecognizedAttribute(string name, string value)
        {
            _settings.Add(name, value);

            return true;
        } 

        #endregion
    }
}
